<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>原型</title>
</head>

<body>
    <script>
        // function Person() {}
        // Person.prototype.name = 'issac'
        // Person.prototype.age = 25
        // Person.prototype.job = "web Engineer";
        // Person.prototype.sayName = function () {
        //     console.log(this.name);
        // };
        // let p1 = new Person()
        // p1.name = 'lilei'
        // p1.sayName()
        // let p2 = new Person()
        // p2.sayName()
        function Person(name, age, job) {
            this.name = name
            this.age = age
            this.job = job
            this.sayName = function () {
                console.log(`Hello everybody i am ${this.name}`)
            }
            this.sayAge = function () {
                console.log(`I am ${this.age} years old`)
            }
            Person.prototype.sayName = function () {
                console.log(`Hello everybody i am ${this.name}`)
            }
            Person.prototype.sayAge = function () {
                console.log(`I am ${this.age} years old`)
            }
        }
        Person.prototype.hello = function(){
            console.log('hello world')
        }
        let person1 = new Person('lilei', 28, 'Java Engineer')
        let person2 = new Person('lily', 25, 'web Engineer')

        Array.prototype.hello = function(){
            console.log('hello world')
        }
        Array.prototype.sum = function(){
           let total = 0;
           this.forEach(function(item){
               if(!isNaN(item)){
                total += item
               }
           })
           return total
        }
        Array.prototype.multiply = function(num){
            let res = []
            this.forEach(function(n){
                if( !isNaN(n)){
                    res.push(n*num) 
                }else{
                    res.push(n)
                } 
            })
            return res 
        }
        let arr = [1,2,3]
        arr.multiply(2)
        //原型的用途
        //1.可以在创建构造函数的时候把共有属性和方法放在原型上，节约内存。
        //2.方便修改和统一管理实例的共同方法或者属性
        //3.以后可以随时添加方法

        
    </script>
</body>

</html>